package cn.tedu.apps_system.service.impl;

import cn.tedu.apps_system.base.response.JsonResult;
import cn.tedu.apps_system.base.response.ResultCode;
import cn.tedu.apps_system.mapper.CartMapper;
import cn.tedu.apps_system.pojo.entity.Cart;
import cn.tedu.apps_system.pojo.vo.UserVO;
import cn.tedu.apps_system.service.CartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CartServicelmpl implements CartService {
    private static final Logger log = LoggerFactory.getLogger(CartServicelmpl.class);
    @Autowired
    private CartMapper cartMapper;

    @Override

    public JsonResult addCart(Cart cart) {
        try {
            if (cart == null || cart.getUser_id() == null || cart.getProduct_id() == null) {
                return new JsonResult(ResultCode.FAILED);
            }
            int s = cartMapper.addCart(cart);
            cart.setCreate_time(new Date());
            cart.setUpdate_time(new Date());
            cart.setSelected(1);
            if (s > 0){
                return JsonResult.ok();
            }
            return new JsonResult(ResultCode.FAILED);
        } catch (Exception e) {
            log.error("添加购物车失败", e);
            return new JsonResult(ResultCode.FAILED);
        }
    }
}
